package com.nextgenapp.fbgraph
{
	import com.nextgenapp.fbgraph.basic.DeleteData;
	import com.nextgenapp.fbgraph.basic.GetData;
	import com.nextgenapp.fbgraph.basic.PostData;
	import com.nextgenapp.fbgraph.util.OAuthUtil;
	
	/**
	 * underlying class to call graph api.
	 * 
	 * all response from graph api are json object.  
	 * the code will json parse the response and place it in FbCallbackResult.result object.  
	 * 
	 * it is highly recommended that one reads the one-page graph api doc: http://developers.facebook.com/docs/api
	 * 
	 * @see http://developers.facebook.com/docs/api
	 * 
	 * @example 
	 * 
	 * <listing version="3.0">
	 * 
	 * private function testRead():void {
	 *     // read data (http get)
	 *     var graphApi:GraphApi = new GraphApi();
	 *     graphApi.read("http://graph.facebook.com/me/friends", getFriendsCallback);
	 * }
	 * 
	 * private function getFriendsCallback(result:FbCallbackResult):void {
	 *     if (result.success) {
	 *         // result.result  will be an object that is translated from json object.  
	 *         Alert.show(result.result);
	 *     } else {
	 *         Alert.show(result.faultEvent.fault.faultString);
	 *     }
	 * } 
	 * </listing>
	 */
	public class GraphApi
	{
		public var access_token:String = null;
		
		public function GraphApi()
		{
		}

		/**
		 * get data from facebook.  
		 * using http get.
		 * 
		 * @param callback - a function like   callback(result:FbCallbackResult).  
		 * 					the specified callback function will be called with parameter FbCallbackResult.  
		 * 					success or failure can be seen from FbCallbackResult.  
		 * 
		 * @see FbCallbackResult
		 */
		public function read(url:String, callback:Function=null):void {
			var gd:GetData = new GetData(callback);
			gd.apiUrl = url;
			if (access_token == null) {
				access_token = OAuthUtil.retrieveAccessToken();
			}
			gd.access_token = access_token;
			gd.getData();
		}
		
		/**
		 * post data to facebook.
		 * using http post.
		 * 
		 * @param callback - a function like   callback(result:FbCallbackResult).  
		 * 					the specified callback function will be called with parameter FbCallbackResult.  
		 * 					success or failure can be seen from FbCallbackResult.  
		 * 
		 * @see FbCallbackResult
		 */
		public function write(url:String, parameters:Object=null, callback:Function=null):void {
			var pd:PostData = new PostData(callback);
			pd.apiUrl = url;
			pd.access_token = OAuthUtil.retrieveAccessToken();
			pd.postData(parameters);
		}
		
		/**
		 * delete data. 
		 * using http delete.
		 * 
		 */
		public function remove(url:String, callback:Function=null):void {
			var dd:DeleteData = new DeleteData(callback);
			dd.apiUrl = url;
			dd.access_token = OAuthUtil.retrieveAccessToken();
			dd.deleteData();
		}
		
	}
}